<?php
/**
 * This class encapsulates various validation functionality.
 *
 * PHP developers should always validate data on the server
 * side to prevent Problems. JavaScript alone is not a good
 * idea for data validation.
 * 
 * Usage:
 * <code>
 * <?php
 * require_once 'validation.class.php';
 * $problem = "";
 * if (! valid::isDate(valid::getFormValue("name"))) {
 *    $problem .= "First name is required<br />";
 * }
 * print $problem;
 * 
 * var_dump(valid::getBooleanValue('-1'));
 * ?>
 *  <form action="" method="get">
 *      <input type="text" name="name" id="name">
 *      <input type="submit" value="Test">
 *  </form>
 * </code>
 *
 * @version 1.0
 * @author Gombos Lorand - Gyula (gombos.lorand@gmail.com)
 */
class valid {
/**
 * Checks the length of a value
 *
 * @param string  $value The value to check
 * @param integer $maxLength The maximum allowable length of the value
 * @param integer $minLength [Optional] The minimum allowable length
 * @return boolean TRUE if the requirements are met, FALSE if not
 */
    public static function checkLength($value, $maxLength, $minLength = 0) {
        if (! (strlen ( $value ) > $maxLength) && ! (strlen ( $value ) < $minLength)) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Compares two values for equality
     *
     * @param string  $value1 First value to compare
     * @param string  $value2 Second value to compare
     * @param boolean $caseSensitive [Optional] TRUE if compare is case sensitive
     * @return boolean TRUE if the values are equal and FALSE if not
     */
    public static function compare($value1, $value2, $caseSensitive = false) {
        if ($caseSensitive) {
            return ($value1 == $value2 ? true : false);
        }
        elseif (strtoupper ( $value1 ) == strtoupper ( $value2 )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Converts any value of any datatype into boolean (true or false)
     *
     * @param any $value Value to analyze for TRUE or FALSE
     * @param any $includeTrueValue (Optional) return TRUE if the value equals this
     * @param any $includeFalseValue (Optional) return FALSE if the value equals this
     * @return boolean Returns TRUE or FALSE
     */
    public static function getBooleanValue($value, $includeTrueValue = null, $includeFalseValue = null) {

        if (! (is_null ( $includeTrueValue )) && $value == $includeTrueValue) {
            return true;
        }
        elseif (! (is_null ( $includeFalseValue )) && $value == $includeFalseValue) {
            return false;
        }
        else {
            if (gettype ( $value ) == "boolean") {
                return ($value == true);
            }
            elseif (is_numeric ( $value )) {
                return ($value > 0);
            }
            else {
                $cleaned = strtoupper ( trim ( $value ) );
                if ($cleaned == "ON") {
                    return true;
                }
                elseif ($cleaned == "SELECTED" || $cleaned == "CHECKED") {
                    return true;
                }
                elseif ($cleaned == "YES" || $cleaned == "Y") {
                    return true;
                }
                elseif ($cleaned == "TRUE" || $cleaned == "T") {
                    return true;
                }
                else {
                    return false;
                }
            }
        }
    }

    /**
     * Get the value for a cookie by the cookie name
     *
     * @param string  $name The name of the cookie
     * @param string  $default (Optional) A default if the value is empty
     * @return string The cookie value
     */
    public static function getCookieValue($name, $default = '') {
        if (isset ( $_COOKIE [$name] )) {
            return $_COOKIE [$name];
        }
        else {
            return $default;
        }
    }

    /**
     * Returns the value if one exists, otherwise returns a default value
     * (This also works on NULL values)
     *
     * @param string  $name The value to check
     * @param string  $default A default if the value is empty
     * @return string Returns the original value unless this value is
     *                empty - in which the default is returned
     */
    public static function getDefaultOnEmpty($value, $default) {
        if (self::hasValue ( $value )) {
            return $value;
        } else {
            return $default;
        }
    }

    /**
     * Get a POST or GET value by a form element name
     *
     * @param string  $name The name of the POST or GET data
     * @param string  $default (Optional) A default if the value is empty
     * @return string The value of the form element
     */
    public static function getFormValue($name, $default = '') {
        if (isset ( $_POST [$name] )) {
            return $_POST [$name];
        }
        elseif (isset ( $_GET [$name] )) {
            return $_GET [$name];
        }
        else {
            return $default;
        }
    }

    /**
     * Get the value for a GET by name
     *
     * @param string  $name The name of the GET
     * @param string  $default (Optional) A default if the value is empty
     * @return string The GET value
     */
    public static function getGetValue($name, $default = '') {
        if (isset ( $_GET [$name] )) {
            return $_GET [$name];
        }
        else {
            return $default;
        }
    }

    /**
     * Get the value for a POST by name
     *
     * @param string  $name The name of the POST
     * @param string  $default (Optional) A default if the value is empty
     * @return string The POST value
     */
    public static function getPostValue($name, $default = '') {
        if (isset ( $_POST [$name] )) {
            return $_POST [$name];
        }
        else {
            return $default;
        }
    }

    /**
     * Get the value for a session by the session name
     *
     * @param string  $name The name of the session
     * @param string  $default (Optional) A default if the value is empty
     * @return string The session value
     */
    public static function getSessionValue($name, $default = '') {
        if (isset ( $_SESSION [$name] )) {
            return $_SESSION [$name];
        }
        else {
            return $default;
        }
    }

    /**
     * Get a POST, GET, Session, or Cookie value by name
     * (in that order - if one doesn't exist, the next is tried)
     *
     * @param string  $name The name of the POST, GET, Session, or Cookie
     * @param string  $default (Optional) A default if the value is empty
     * @return string The value from that element
     */
    public static function getValue($name, $default = '') {
        if (isset ( $_POST [$name] )) {
            return $_POST [$name];
        }
        elseif (isset ( $_GET [$name] )) {
            return $_GET [$name];
        }
        elseif (isset ( $_SESSION [$name] )) {
            return $_SESSION [$name];
        }
        elseif (isset ( $_COOKIE [$name] )) {
            return $_COOKIE [$name];
        }
        else {
            return $default;
        }
    }

    /**
     * Checks to see if a variable contains a value
     *
     * @param string  $value The value to check
     * @return boolean TRUE if a value exists, FALSE if empty
     */
    public static function hasValue($value) {
        if (strlen ( $value ) < 1 || is_null ( $value ) || empty ( $value )) {
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * Determines if a string is alpha only
     *
     * @param string $value The value to check for alpha (letters) only
     * @param string $allow Any additional allowable characters
     * @return boolean
     */
    public static function isAlpha($value, $allow = '') {
        if (preg_match ( '/^[a-zA-Z' . $allow . ']+$/', $value )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Determines if a string is alpha-numeric
     *
     * @param string $value The value to check
     * @return boolean TRUE if there are letters and numbers, FALSE if other
     */
    public static function isAlphaNumeric($value) {
        if (preg_match ( "/^[A-Za-z0-9 ]+$/", $value )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Determines if a string is a CNP
     *
     * @param string $value The value to check
     * @return boolean TRUE if the $value is CNP, FALSE if other
     */
    public static function isCnp($value) {
    	if (strlen($value) != 13){
    		return false;
    	}
    	if((int)substr($value,0,1) < 1 OR (int)substr($$value,0,1) > 6){
    		if ((int)substr($valuep,0,1) != 9){
    			return false;
    		}
    	}
    	$cheiaTestare = array ('2','7','9','1','4','6','3','5','8','2','7','9');
    	$cnpArray = array();

    	for ($i=0; $i<strlen($value); $i++){
    		$cnpArray[] = substr($value,$i,1);
    	}

    	$suma =
    	(int)$cnpArray[0] *(int)$cheiaTestare[0] +
    	(int)$cnpArray[1] *(int)$cheiaTestare[1]+
    	(int)$cnpArray[2] *(int)$cheiaTestare[2]+
    	(int)$cnpArray[3] *(int)$cheiaTestare[3]+
    	(int)$cnpArray[4] *(int)$cheiaTestare[4]+
    	(int)$cnpArray[5] *(int)$cheiaTestare[5]+
    	(int)$cnpArray[6] *(int)$cheiaTestare[6]+
    	(int)$cnpArray[7] *(int)$cheiaTestare[7]+
    	(int)$cnpArray[8] *(int)$cheiaTestare[8]+
    	(int)$cnpArray[9] *(int)$cheiaTestare[9]+
    	(int)$cnpArray[10]*(int)$cheiaTestare[10]+
    	(int)$cnpArray[11]*(int)$cheiaTestare[11];

    	$rest = (int)$suma%11;

    	if ($rest < 10 && $rest != $cnpArray[12]){
    		return false;
    	}
    	elseif($rest == 10 && $cnpArray[12] != 1){
    		return false;
    	}
    	else{
    		return true;
    	}
    }

    /**
     * Determines if a string contains a valid date
     * <code>
     *      mm/dd/yyyy - 02/01/2009  - strtotime() returns : 1st February 2009
     *      mm/dd/yy   - 02/01/09    - strtotime() returns : 1st February 2009
     *      yyyy/mm/dd - 2009/02/01  - strtotime() returns : 1st February 2009
     *      dd-mm-yyyy - 01-02-2009  - strtotime() returns : 1st February 2009
     *      yy-mm-dd   - 09-02-01    - strtotime() returns : 1st February 2009
     *      yyyy-mm-dd - 2009-02-01  - strtotime() returns : 1st February 2009
     * </code>
     * @param string $value The value to inspect
     * @return boolean TRUE if the value is a date, FALSE if not
     */
    public static function isDate($value) {
        if (!valid::checkLength($value, 10, 10)) {
            if (!valid::checkLength($value, 8, 8)) {
                return false;
            }
        }
        $date = date ( 'Y-m-d', strtotime ( $value ) );
        if ($date == '1970-01-01') {
            return false;
        }
        return true;
    }

    public static function isDateTime($value) {

    }

    /**
     * Checks for a valid email address
     *
     * @param string  $email The value to validate as an email address
     * @return boolean TRUE if it is a valid email address, FALSE if not
     */
    public static function isEmail($email) {
        $pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";

        if (preg_match ( $pattern, $email )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Checks to see if a variable contains no value (not even a zero)
     *
     * @param string  $value The value to check
     * @return boolean TRUE if a value exists, FALSE if empty
     */
    public static function isEmpty($value) {
        if (strlen ( $value ) < 1 || is_null ( $value )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Checks for a valid internet URL
     *
     * @param string $value The value to check
     * @return boolean TRUE if the value is a valid URL, FALSE if not
     */
    public static function isInternetURL($value) {
        if (preg_match ( "/^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/i", $value )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Checks for a valid IP Address
     *
     * @param string $value The value to check
     * @return boolean TRUE if the value is an IP address, FALSE if not
     */
    public static function isIPAddress($value) {
        $pattern = "/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i";
        if (preg_match ( $pattern, $value )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Checks to see if a variable is a number
     *
     * @param integer $number The value to check
     * @return boolean TRUE if the value is a number, FALSE if not
     */
    public static function isNumber($number) {
        if (preg_match ( "/^\-?\+?[0-9e1-9]+$/", $number )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Checks for a two character state abbreviation
     *
     * @param string $value The value to inspect
     * @return boolean TRUE if the value is a 2 letter state abbreviation
     *                 FALSE if the value is anything else
     */
    public static function isStateAbbreviation($value) {
        if (preg_match ( "/^[A-Z][A-Z]$/i", $value )) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Check to see if a string length is too long
     *
     * @param string $value The string value to check
     * @param integer $maximumLength The maximum allowed length
     * @return boolean TRUE if the length is too long
     *                 FALSE if the length is acceptable
     */
    public static function isTooLong($value, $maximumLength) {
        if (self::checkLength ( $value, $maximumLength )) {
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * Check to see if a string length is too short
     *
     * @param string $value The string value to check
     * @param integer $maximumLength The minimum allowed length
     * @return boolean TRUE if the length is too short
     *                 FALSE if the length is acceptable
     */
    public static function isTooShort($value, $minimumLength) {
        if (strlen ( $value ) < $minimumLength) {
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * Checks to see if a variable is an unsigned number
     *
     * @param integer $number The value to inspect
     * @return boolean TRUE if the value is a number without a sign
     *                 and FALSE if a sign exists
     */
    public static function isUnsignedNumber($number) {
        if (preg_match ( "/^\+?[0-9]+$/", $number )) {
            return true;
        }
        else {
            return false;
        }
    }

}
?>